package com.dq.controller.app.goods;

import com.alibaba.fastjson.JSONObject;
import com.dq.controller.in.goods.stock.OrgStockCheckQuery;
import com.dq.controller.in.goods.stock.OrgGoodsAndStockQuery;
import com.dq.domain.record.stockcheck.StockCheckRecord;
import com.dq.exception.customException.AdminException;
import com.dq.service.AllService;
import com.dq.utils.TimeUtil;
import com.dq.vo.ListDataVo;
import com.dq.vo.Vo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author cf
 */
@RestController
@RequestMapping("/app/goods/stockCheck/")
@Api(description = "库存盘点管理")
public class AppStockCheckController extends AllService {

    @PostMapping("stockCheckData")
    @ApiOperation("门店库存盘点-数据展示")
    public Vo stockCheckData(@RequestBody OrgStockCheckQuery orgStockCheckQuery) throws AdminException {
        // 关联门店, 门店ID, 不传默认是登录账号门店下
        Long orgId = orgStockCheckQuery.getOrgId() == null ? sysUserService.getUserInSection().getSection().getId() : orgStockCheckQuery.getOrgId();

        Long goodsCount = goodsService.goodsByOrgCount(new OrgGoodsAndStockQuery(), orgId, false);
        OrgStockCheckQuery nullQuery = new OrgStockCheckQuery();
        nullQuery.setOrgId(orgId);
        Long stockAll = stockChangeRecordService.orgStockAll(nullQuery);
        Long expendAll = stockChangeRecordService.orgExpendAll(nullQuery);
        Long sellAll = stockChangeRecordService.orgSellAll(nullQuery);

        Long realStock = stockChangeRecordService.orgRealStockAll(nullQuery);

        Long monthBegin = TimeUtil.getThisMonthFirstDayZeroTimestamp();
        Long monthEnd = TimeUtil.getThisMonthEndTimestamp();
        OrgStockCheckQuery queryMonth = new OrgStockCheckQuery();
        queryMonth.setBeginTime(monthBegin);
        queryMonth.setEndTime(monthEnd);
        queryMonth.setOrgId(orgId);
        Long expendMonth = stockChangeRecordService.orgExpendAll(queryMonth);
        Long sellMonth = stockChangeRecordService.orgSellAll(queryMonth);
        BigDecimal sellPriceMonth = stockChangeRecordService.orgSellPriceAll(queryMonth);

        Long todayBegin = TimeUtil.getTodayZeroTimestamp();
        Long todayEnd = TimeUtil.getTodayEndTimestamp();
        OrgStockCheckQuery queryDay = new OrgStockCheckQuery();
        queryDay.setBeginTime(todayBegin);
        queryDay.setEndTime(todayEnd);
        queryDay.setOrgId(orgId);
        Long expendDay = stockChangeRecordService.orgExpendAll(queryDay);
        Long sellDay = stockChangeRecordService.orgSellAll(queryDay);
        BigDecimal sellPriceDay = stockChangeRecordService.orgSellPriceAll(queryDay);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("goodsCount", goodsCount);
        jsonObject.put("stockAll", stockAll);
        jsonObject.put("expendAll", expendAll);
        jsonObject.put("sellAll", sellAll);
        jsonObject.put("realStock", realStock);
        jsonObject.put("expendMonth", expendMonth);
        jsonObject.put("sellMonth", sellMonth);
        jsonObject.put("sellPriceMonth", sellPriceMonth);
        jsonObject.put("expendDay", expendDay);
        jsonObject.put("sellDay", sellDay);
        jsonObject.put("sellPriceDay", sellPriceDay);

        return new Vo(jsonObject);
    }

    @PostMapping("listInfoAndStock")
    @ApiOperation(value = "库存盘点详情列表", hidden = true)
    public Vo listInfoAndStock(@RequestBody OrgStockCheckQuery orgStockCheckQuery) throws AdminException {
        orgStockCheckQuery.format();
        List<StockCheckRecord> list = stockCheckRecordService.list(orgStockCheckQuery);

        List<StockCheckRecord.Vo> vos = new ArrayList<>();
        for (StockCheckRecord stockCheckRecord : list) {
            StockCheckRecord.Vo vo = (StockCheckRecord.Vo) stockCheckRecord.toVO();
            Long stock = goodsService.productStock(stockCheckRecord.getGoods().getId(), vo.getOrgId());
            vo.setRealstock(stock);
            vos.add(vo);
        }

        return new ListDataVo(vos, stockCheckRecordService.count(orgStockCheckQuery));
    }

}
